<?php

namespace catchAdmin\shenhe\controller;

use catchAdmin\permissions\model\Users;
use catcher\base\CatchController;
use catcher\CatchResponse;
use think\facade\Db;

class Shenhe extends CatchController
{

    public function shenhe()
    {

        Db::name('jmj')->where('id', request()->param('id'))->update(['shenhe' => 1]);
        return CatchResponse::success();
    }


    public function getJmjList()
    {


        $page = request()->param();

        $data = Db::name('jmj')->wherelike('name', $page['name'])->order('id', 'desc')->paginate([
            'list_rows' => $page['size'],
            'page' => $page['page'],
        ])->toArray();


        foreach ($data['data'] as &$v) {
            $v['a1'] = Db::name('jm')->where('uid', $v['id'])->count();
            $v['a2'] = Db::name('jm')->where('uid', $v['id'])->where('fasong', 1)->count();
            $v['a3'] = Db::name('jm')->where('uid', $v['id'])->where('fasong', 0)->count();
            $v['a4'] = Db::name('jm')->where('uid', $v['id'])->where('shenhe', 0)->count();
            $v['a5'] = Users::where('id', $v['creator_id'])->find()['username'];

        }
        return CatchResponse::success($data, '提交成功', 10000);
    }

    public function shenhejm()
    {
        Db::name('jm')->where('id', request()->param('id'))->update(['shenhe' => 1]);
        return CatchResponse::success();
    }


    public function send($id)
    {
        $data = Db::name('jmj')->where('id', $id)->find();
        $data['programs'] = Db::name('jm')->where('uid', $data['id'])->where('shenhe',1)->where('fasong',0)->select()->toArray();

        foreach ($data['programs'] as &$v) {
            // 设置 program 的 id
            $v['id'] = $v['uuid'];

            // 初始化 videos 数组
            $videos = [];

            // 假设每个 program 只有一个 video，将其放入数组中
            $video = [
                'definition' => $v['definition'],
                'videoDataRate' => $v['videoDataRate'],
                'videoSize' => $v['videoSize'],
                'lengthUnit' => $v['lengthUnit'],
                'videoLength' => $v['videoLength'],
                'headLength' => $v['headLength'],
                'tailLength' => $v['tailLength'],
                'videoPaths' => [$v['videoPaths']],
                'imgPaths' => [$v['imgPaths']],
                'createTime' => $v['createTime'],
                'videoPart' => $v['videoPart'],
                'videoPartTime' => $v['videoPartTime'],
                'videoNameCodeF' => $v['videoNameCodeF'],
                'videoNameCodeB' => $v['videoNameCodeB'],
                'templateId' => $v['templateId'],
                'priority' => $v['priority'],
            ];

            // 将 video 添加到 videos 数组中
            $videos[] = $video;

            // 将 videos 数组赋值给 $v['videos']
            $v['videos'] = $videos;
            unset($v['uid']);
        }

// 确保所有引用都被解除
//        unset($v);

//        p($data);
//        dd();

        $data['id'] =  $data['UUID'];
        $res = sendPostRequest('http://media.cloud.ottcn.com/piper/service/program/plys/push', $data);
        $res = json_decode(json_encode($res['response']), true);
        $res = json_decode($res, true);


//        p($res);
        if ($res['msg'] == "success"){
           Db::name('jm')->where('field101', $data['name'])->where('shenhe',1)->update(['fasong' => 1]);
        }

        return CatchResponse::success($data);

    }
}
